/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.yokeyword.sample.demo_flow.base;

import me.yokeyword.fragmentation.ISupportFraction;
import me.yokeyword.fragmentation.SupportFractionDelegate;

import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.utils.PacMap;

/**
 * The customized MySupportFraction is displayed. SupportFraction is not inherited.
 */
public class MySupportFraction extends Fraction implements ISupportFraction {
    final SupportFractionDelegate mDelegate = new SupportFractionDelegate(this);

    @Override
    public SupportFractionDelegate getSupportDelegate() {
        return mDelegate;
    }

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        return super.onComponentAttached(scatter, container, intent);
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        mDelegate.onStart(intent);
    }

    @Override
    protected void onInactive() {
        super.onInactive();
        mDelegate.onInactive();
    }

    @Override
    protected void onActive() {
        super.onActive();
        mDelegate.onActive();
        mDelegate.onComponentAttached(getFractionAbility());
    }

    @Override
    protected void onComponentDetach() {
        mDelegate.onDestroyView();
        super.onComponentDetach();
    }

    @Override
    protected void onStop() {
        mDelegate.onDestroy();
        super.onStop();
    }

   /**
     * Causes the Runnable r to be added to the action queue.
     * <p>
     * The runnable will be run after all the previous action has been run.
     * <p>
     * 前面的事务全部执行后 执行该Action
    * @param runnable runnable to run in UI thread
     */
    @Override
    public void post(Runnable runnable) {
        mDelegate.post(runnable);
    }

    @Override
    public void setFractionResult(int resultCode, PacMap bundle) {
        mDelegate.setFractionResult(resultCode, bundle);
    }

    @Override
    public void onFractionResult(int requestCode, int resultCode, PacMap data) {

    }

    @Override
    public void onNewBundle(PacMap args) {
        mDelegate.onNewBundle(args);
    }

    @Override
    public void putNewBundle(PacMap newBundle) {
        mDelegate.putNewBundle(newBundle);
    }

    /**
     * 按返回键触发,前提是SupportActivity的onBackPressed()方法能被调用
     *
     * @return false则继续向上传递, true则消费掉该事件
     */
    @Override
    public boolean onBackPressedSupport() {
        return mDelegate.onBackPressedSupport();
    }

    @Override
    public void setArguments(PacMap newBundle) {
        mDelegate.setArguments(newBundle);
    }

    @Override
    public PacMap getArguments() {
        return mDelegate.getArguments();
    }

     /**
     * 加载根Fraction, 即Activity内的第一个Fraction 或 Fraction内的第一个子Fraction
     *
     * @param containerId 容器id
     * @param toFraction  目标Fraction
     */
    public void loadRootFraction(int containerId, ISupportFraction toFraction) {
        mDelegate.loadRootFraction(containerId, toFraction);
    }

    public void loadRootFraction(int containerId, ISupportFraction toFraction, boolean addToBackStack, boolean allowAnim) {
        mDelegate.loadRootFraction(containerId, toFraction, addToBackStack, allowAnim);
    }

    public void start(ISupportFraction toFraction) {
        mDelegate.start(toFraction);
    }

    /**
     * start new fraction with launch modes
     * @param launchMode Similar to Ability's LaunchMode.
     * @param toFraction start new fraction
     */
    public void start(final ISupportFraction toFraction, int launchMode) {
        mDelegate.start(toFraction, launchMode);
    }

    /**
     * Launch an fraction for which you would like a result when it poped.
     * @param toFraction new fraction
     * @param requestCode request code
     */
    public void startForResult(ISupportFraction toFraction, int requestCode) {
        mDelegate.startForResult(toFraction, requestCode);
    }

    /**
     * Start the target Fraction and pop itself
     * @param toFraction pop and start new fraction
     */
    public void startWithPop(ISupportFraction toFraction) {
        mDelegate.startWithPop(toFraction);
    }

    public void pop() {
        mDelegate.pop();
    }

    /**
     * Pop the last fraction transition from the manager's fraction
     * back stack.
     * <p>
     * 出栈到目标fraction
     *
     * @param targetFractionClass   目标fraction
     * @param includeTargetFraction 是否包含该fraction
     */
    public void popTo(Class<?> targetFractionClass, boolean includeTargetFraction) {
        mDelegate.popTo(targetFractionClass, includeTargetFraction);
    }
}
